package com.hypo.business.controller;

import com.hypo.business.domain.Result;
import com.hypo.business.domain.dto.*;
import com.hypo.business.domain.po.SysDictData;
import com.hypo.business.domain.po.Warehouse;
import com.hypo.business.domain.po.WarehouseArea;
import com.hypo.business.domain.po.WarehouseAreaLocal;
import com.hypo.business.service.SysDictDataService;
import com.hypo.business.service.WarehouseAreaLocalService;
import com.hypo.business.service.WarehouseAreaService;
import com.hypo.business.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hypo
 * @version 1.0
 * @since 2025/8/13
 */
@RestController
@RequestMapping("/warehouse")
@RequiredArgsConstructor
public class WarehouseController {

    private final WarehouseService warehouseService;

    private final WarehouseAreaService warehouseAreaService;

    private final WarehouseAreaLocalService warehouseAreaLocalService;

    private final SysDictDataService sysDictDataService;

    /**
     * 删除库位信息
     */
    @DeleteMapping("/local/{id}")
    public Result deleteLocal(@PathVariable Long id) {
        warehouseAreaLocalService.removeById(id);
        return Result.success();
    }

    /**
     * 修改库位信息
     */
    @PutMapping("/local")
    public Result warehouseAreaLocalUpdate(@RequestBody WarehouseAreaLocalOpDto dto) {
        warehouseAreaLocalService.warehouseAreaLocalUpdate(dto);
        return Result.success();
    }

    /**
     * 新增库位信息
     */
    @PostMapping("/local")
    public Result warehouseAreaLocalAdd(@RequestBody WarehouseAreaLocalOpDto dto) {
        warehouseAreaLocalService.warehouseAreaLocalAdd(dto);
        return Result.success();
    }

    /**
     * 根据库区id查询库区类型
     */
    @GetMapping("/area/type/{id}")
    public Result warehouseAreaType(@PathVariable Integer id) {
        SysDictData sysDictData = sysDictDataService.getById(id);
        return Result.success(sysDictData);
    }

    /**
     * 根据仓库Id查询库区
     */
    @GetMapping("/area/{warehouseId}")
    public Result warehouseAreaById(@PathVariable Integer warehouseId) {
        List<WarehouseArea> warehouseArea = warehouseAreaService.queryWarehouseAreaById(warehouseId);
        return Result.success(warehouseArea);
    }

    /**
     * 库位分页查询
     */
    @GetMapping("/local")
    public Result warehouseAreaLocalPage(WarehouseAreaLocalPageDto dto) {
        PageResultDto<WarehouseAreaLocal> page = warehouseAreaLocalService.warehouseAreaLocalPage(dto);
        return Result.success(page);
    }

    /**
     * 查询所有库区
     */
    @GetMapping("/area/list")
    public Result warehouseAreaList() {
        List<WarehouseArea> warehouseArea = warehouseAreaService.queryWarehouseArea();
        return Result.success(warehouseArea);
    }

    /**
     * 删除库区
     */
    @DeleteMapping("/area/{id}")
    public Result warehouseAreaDelete(@PathVariable Integer id) {
        int delete = warehouseAreaService.warehouseAreaDelete(id);
        if (delete == 0) {
            return Result.error("删除失败！");
        }
        return Result.success();
    }

    /**
     * 库区修改
     */
    @PutMapping("/area")
    public Result warehouseAreaUpdate(@RequestBody WarehouseAreaOpDto dto) {
        warehouseAreaService.warehouseAreaUpdate(dto);
        return Result.success();
    }

    /**
     * 库区添加
     */
    @PostMapping("/area")
    public Result warehouseAreaAdd(@RequestBody WarehouseAreaOpDto dto) {
        warehouseAreaService.warehouseAreaAdd(dto);
        return Result.success();
    }

    /**
     * 查询数据字典库区
     */
    @GetMapping("/dictData")
    public Result warehouseDictData() {
        List<SysDictData> dictData = sysDictDataService.queryDictData();
        return Result.success(dictData);
    }

    /**
     * 库区分页查询
     */
    @GetMapping("/area")
    public Result warehouseAreaPage(WarehouseAreaPageDto dto) {
        PageResultDto<WarehouseArea> page = warehouseAreaService.warehouseAreaPage(dto);
        return Result.success(page);
    }

    /**
     * 查询全部仓库
     */
    @GetMapping("/listAll")
    public Result allWarehouse() {
        List<Warehouse> warehouse = warehouseService.queryWarehouse();
        return Result.success(warehouse);
    }

    /**
     * 仓库分页查询
     */
    @GetMapping("/list")
    public Result warehousePage(WarehousePageDto dto) {
        PageResultDto<Warehouse> page = warehouseService.warehousePage(dto);
        return Result.success(page);
    }

    /**
     * 仓库添加
     */
    @PostMapping
    public Result warehouseAdd(@RequestBody WarehouseOpDto dto) {
        warehouseService.warehouseAdd(dto);
        return Result.success();
    }

    /**
     * 仓库修改
     */
    @PutMapping
    public Result warehouseUpdate(@RequestBody WarehouseOpDto dto) {
        warehouseService.warehouseUpdate(dto);
        return Result.success();
    }

    /**
     * 仓库删除
     */
    @DeleteMapping("/{id}")
    public Result warehouseDelete(@PathVariable Integer id) {
        int delete = warehouseService.warehouseDelete(id);
        if (delete == 0) {
            return Result.error("删除失败！");
        }
        return Result.success();
    }
}
